#!/bin/bash

mkdir -p ../../3rdParty/build
pushd ../../3rdParty/build

FETCH="curl -f"
FETCH_URL="http://sf.anu.edu.au/~rph900/drishti/3rdParty"
INSTALL_PREFIX=`pwd`/..

MACHINE=`uname -m`

if [ "$MACHINE" == "x86_64" ]; then
  echo "64 bit"
  pushd ..
  rm -rf lib64
  mkdir -p lib
  ln -s lib lib64
  popd
fi

prep() {
  tgz=$1
  dir=$2
  if [ -z $3 ]; then
    decomp=z
  else
    decomp=$3
  fi

  if [ ! -e $tgz ]; then
    echo "Fetching $tgz"
    $FETCH $FETCH_URL/$tgz > $tgz || exit
  fi
  if [ ! -d $dir ]; then
    echo "Extracting $tgz"
    tar -x -$decomp -f $tgz || exit
  fi

  echo "Prepared $dir"
}




#
# Qt
#
QT_TGZ="qt-x11-opensource-src-4.5.3.tar.gz"
QT_DIR="qt-x11-opensource-src-4.5.3"
prep $QT_TGZ $QT_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libQtCore.la ]; then
  pushd $QT_DIR
  ./configure -prefix $INSTALL_PREFIX -opensource || exit
  make || exit
  make install || exit
  popd
fi

echo "$QT_DIR up-to-date"

#
# NetCDF
#
NETCDF_TGZ=netcdf-4.0.1.tar.gz
NETCDF_DIR=netcdf-4.0.1
prep $NETCDF_TGZ $NETCDF_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libnetcdf.la ]; then
  pushd $NETCDF_DIR
  ./configure --disable-static --prefix=$INSTALL_PREFIX || exit
  make || exit
  make install || exit
  popd
fi

echo "$NETCDF_DIR up-to-date"

#
# JPEG
#
JPEG_TGZ=jpegsrc.v6b.tar.gz
JPEG_DIR=jpeg-6b
prep $JPEG_TGZ $JPEG_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libjpeg.a ]; then
  pushd $JPEG_DIR
  if [ $MACHINE == "x86_64" ]; then
    CFLAGS=-fpic ./configure --prefix=$INSTALL_PREFIX || exit
  else
    ./configure --prefix=$INSTALL_PREFIX || exit
  fi
  make || exit
  # Broken install fix
  mkdir -p $INSTALL_PREFIX/man/man1 || exit
  make install || exit
  make install-lib || exit
  popd
fi

echo "$JPEG_DIR up-to-date"

#
# HDF
#
HDF_TGZ=HDF4.2r4.tar.gz
HDF_DIR=HDF4.2r4
prep $HDF_TGZ $HDF_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libmfhdf.la ]; then
  pushd $HDF_DIR
  ./configure --disable-fortran --disable-netcdf --prefix=$INSTALL_PREFIX --with-jpeg=$INSTALL_PREFIX || exit
  make || exit
  make install || exit
  popd
fi

echo "$HDF_DIR up-to-date"

#
# QGLViewer
#
QGL_TGZ=libQGLViewer-2.3.2.tar.gz
QGL_DIR=libQGLViewer-2.3.2
prep $QGL_TGZ $QGL_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libQGLViewer.so.2.3.2 ]; then
  pushd $QGL_DIR/QGLViewer
  $INSTALL_PREFIX/bin/qmake PREFIX=$INSTALL_PREFIX || exit 
  make || exit
  make install || exit
  popd
fi

echo "$QGL_DIR up-to-date"

#
# GLEW
#
GLEW_TGZ=glew-1.5.1-src.tgz
GLEW_DIR=glew
prep $GLEW_TGZ $GLEW_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libGLEW.a ]; then
  pushd $GLEW_DIR
  pushd config
  cp config.guess config.guess.orig
  awk '{ sub("\r$", ""); print }' config.guess.orig > config.guess
  popd
  make || exit
  make GLEW_DEST=$INSTALL_PREFIX install
  popd
fi

echo "$GLEW_DIR up-to-date"

#
# ffmpeg
#
FFMPEG_TGZ=ffmpeg-090821.tar.gz
FFMPEG_DIR=ffmpeg
prep $FFMPEG_TGZ $FFMPEG_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libavcodec.so ]; then
  pushd $FFMPEG_DIR
  ./configure --prefix=$INSTALL_PREFIX --enable-shared --enable-gpl || exit
  make || exit
  make install || exit
  popd
fi

#
# libpng
#
LIBPNG_TGZ=libpng-1.2.39.tar.gz
LIBPNG_DIR=libpng-1.2.39
prep $LIBPNG_TGZ $LIBPNG_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libpng.so ]; then
  pushd $LIBPNG_DIR
  ./configure --prefix=$INSTALL_PREFIX || exit
  make || exit
  make install || exit
  popd
fi

#
# glmedia
#
GLM_TGZ=libglmedia-0.1.6.tar.gz
GLM_DIR=libglmedia-0.1.6
prep $GLM_TGZ $GLM_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libglmedia.la ]; then
  pushd $GLM_DIR
  ./configure --prefix=$INSTALL_PREFIX --with-glew=$INSTALL_PREFIX --with-ffmpeg=$INSTALL_PREFIX || exit
  make || exit
  make install || exit
  popd
fi

echo "$GLM_DIR up-to-date"

#
# tiff
#
TIFF_TGZ=tiff-3.8.2.tar.gz
TIFF_DIR=tiff-3.8.2
prep $TIFF_TGZ $TIFF_DIR z

if [ ! -e $INSTALL_PREFIX/lib/libtiff.so ]; then
  pushd $TIFF_DIR
  ./configure --prefix=$INSTALL_PREFIX --with-jpeg-include-dir=$INSTALL_PREFIX/include --with-jpeg-lib-dir=$INSTALL_PREFIX/lib || exit
  make || exit
  make install || exit
  popd
fi

echo "$TIFF_DIR up-to-date"

#
# Image Magick
#
MAGICK_TGZ=ImageMagick-6.5.4-3.tar.bz2 
MAGICK_DIR=ImageMagick-6.5.4-3
prep $MAGICK_TGZ $MAGICK_DIR j

if [ ! -e $INSTALL_PREFIX/lib/libMagickCore.a ]; then
  pushd $MAGICK_DIR
  CPPFLAGS="-I$INSTALL_PREFIX/include" LDFLAGS="-L$INSTALL_PREFIX/lib" ./configure --prefix=$INSTALL_PREFIX --without-perl --without-x --without-freetype --with-tiff --with-jpeg --enable-hdri || exit
  make || exit
  make install || exit
  popd
fi

echo "$MAGICK_DIR up-to-date"


